import {
  check,
  checkMultiple,
  request,
  requestMultiple,
  RESULTS,
  PERMISSIONS,
  openSettings,
} from 'react-native-permissions';
import {Alert, Platform, ToastAndroid as Toast} from 'react-native';
import RNExitApp from 'react-native-exit-app';

export default {
  async requestPermission(permission: string) {
    let result = await check(permission);
    switch (result) {
      case RESULTS.UNAVAILABLE:
        //不支持该功能
        Alert.alert('', '您的设备不支持该功能', [{text: '确定'}]);
        return;
      case RESULTS.DENIED:
        //该权限尚未被请求、被拒绝，但可请求
        let req = await request(permission);
        if (req === RESULTS.DENIED) {
          Toast.show('您已经拒绝授权', Toast.SHORT);
          return;
        }
        if (req === RESULTS.BLOCKED) {
          //该权限被拒绝
          Alert.alert('提示', '您已经禁用相关必须权限,APP使用受限', [
            {
              text: '退出APP',
              onPress: () => {
                RNExitApp.exitApp();
              },
            },
            {
              text: '去设置开启权限',
              onPress: () => {
                openSettings();
              },
            },
          ]);
          return;
        }
        break;
      case RESULTS.GRANTED:
        //授予权限
        break;
      case RESULTS.BLOCKED:
        //该权限被拒绝
        Alert.alert('提示', '您已经禁用相关必须权限,APP使用受限', [
          {
            text: '退出APP',
            onPress: () => {
              RNExitApp.exitApp();
            },
          },
          {
            text: '去设置开启权限',
            onPress: () => {
              openSettings();
            },
          },
        ]);
        return;
    }
  },
  async requestPermissions(permissions: string[]) {
    let results = await checkMultiple(permissions);
    let unavailablePermissions = permissions.filter(
      permission => results[permission] === RESULTS.UNAVAILABLE,
    );
    let deniedPermissions = permissions.filter(
      permission => results[permission] === RESULTS.DENIED,
    );
    let blockedPermissions = permissions.filter(
      permission => results[permission] === RESULTS.BLOCKED,
    );
    if (unavailablePermissions.length > 0) {
      //不支持该功能
      Alert.alert('', '您的设备不支持该功能', [{text: '确定'}]);
      return;
    }
    if (blockedPermissions.length > 0) {
      //该权限被拒绝
      Alert.alert('提示', '您已经禁用相关必须权限,APP使用受限', [
        {
          text: '退出APP',
          onPress: () => {
            RNExitApp.exitApp();
          },
        },
        {
          text: '去设置开启权限',
          onPress: () => {
            openSettings();
          },
        },
      ]);
      return;
    }
    if (deniedPermissions.length > 0) {
      //该权限尚未被请求、被拒绝，但可请求
      let req = await requestMultiple(deniedPermissions);
      let reqDeniedPermissions = deniedPermissions.filter(
        permission => req[permission] === RESULTS.DENIED,
      );
      let reqBlockedPermissions = deniedPermissions.filter(
        permission => req[permission] === RESULTS.BLOCKED,
      );
      if (reqBlockedPermissions.length > 0) {
        //该权限被拒绝
        Alert.alert('提示', '您已经禁用相关必须权限,APP使用受限', [
          {
            text: '退出APP',
            onPress: () => {
              RNExitApp.exitApp();
            },
          },
          {
            text: '去设置开启权限',
            onPress: () => {
              openSettings();
            },
          },
        ]);
      }
      if (reqDeniedPermissions.length > 0) {
        Toast.show('您已经拒绝授权', Toast.SHORT);
      }
    }
  },
  async requestNecessaryPermissions() {
    let permissions = [];
    if (Platform.OS === 'android') {
      permissions = [PERMISSIONS.ANDROID.CAMERA];
    } else if (Platform.OS === 'ios') {
      permissions = [PERMISSIONS.IOS.CAMERA];
    } else {
      Alert.alert('提示', '当前机型暂不支持权限授予', [
        {
          text: '退出APP',
          onPress: () => {
            RNExitApp.exitApp();
          },
        },
      ]);
    }
    await this.requestPermissions(permissions);
  },
};
